﻿using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using wnxd_feature;

namespace Hidistro.UI.Common.Controls
{
    public class wnxd_FeatureSelect : WebControl
    {

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.RendHtml());
        }

        public string RendHtml()
        {
            StringBuilder builder = new StringBuilder();
            int[] selected = new int[0];
            string[] wnxd_feature = new string[0];
            feature wnxd = new feature();
            HttpRequest Request = (HttpContext.Current.Handler as System.Web.UI.Page).Request;
            string cid = Request.QueryString["categoryId"];
            string pid = Request.QueryString["productId"];
            if (!string.IsNullOrEmpty(cid))
            {
                wnxd_feature = wnxd.GetFeature(int.Parse(cid));
            }
            else if (!string.IsNullOrEmpty(pid))
            {
                int productid = int.Parse(pid);
                selected = wnxd.ProductGet(productid);
                wnxd_feature = wnxd.GetFeature(wnxd.ProductId2CategoryId(productid));
            }

            for (int n = 0; n < wnxd_feature.Length; n++)
            {
                if (!string.IsNullOrEmpty(wnxd_feature[n]))
                {
                    builder.AppendFormat("<input class=\"wnxd_feature\" type=\"checkbox\" name=\"wnxd_feature[]\" value=\"{0}\" {2}>{1}", (n + 1).ToString(), wnxd_feature[n], Array.IndexOf(selected, n + 1) != -1 ? "checked=\"checked\"" : "");
                }
            }

            return builder.ToString();
        }
    }
}